package com.wcl.nullmusic.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wcl.nullmusic.R;
import com.wcl.nullmusic.mode.Anthology_Track_List;
import com.wcl.nullmusic.utils.NumberUtils;
import com.wcl.nullmusic.utils.BaseUtils;
import com.wcl.nullmusic.view.CircleTransformation;

import java.util.List;

/**
 * Created by dllo on 16/1/17.
 */
public class SongDetailRecyclerViewAdapter extends RecyclerView.Adapter<SongDetailRecyclerViewAdapter.ViewHolder> {
    private List<Anthology_Track_List> datas;
    private Context context;

    public SongDetailRecyclerViewAdapter(Context context) {
        this.context = context;
    }

    public void addData(List<Anthology_Track_List> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.songdetail_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (datas != null && datas.size() > 0) {
            Anthology_Track_List list= datas.get(position);
            holder.songdetail_item_title.setText(list.getTitle());
            holder.songdetail_item_subTitle.setText("by "+list.getNickname());
            holder.songdetail_item_playerCounts.setText(NumberUtils.toUnit(list.getPlaysCounts()));
            holder.songdetail_item_duration.setText(BaseUtils.toDurtion2((int) list.getDuration()));
            holder.songdetail_item_like.setText(list.getFavoritesCounts()+"");
            holder.songdetail_item_comment.setText(list.getComments()+"");

            Picasso.with(context).load(list.getCoverSmall()).placeholder(R.mipmap.ic_classify_img06)
                    .error(R.mipmap.ic_classify_img06).transform(new CircleTransformation()).into(holder.songdetail_item_pic);

        }
    }

    @Override
    public int getItemCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout songdetail_item_layout;
        private ImageView songdetail_item_pic;
        private TextView songdetail_item_title, songdetail_item_subTitle,
                songdetail_item_playerCounts, songdetail_item_duration,
                songdetail_item_like, songdetail_item_comment;

        public ViewHolder(View itemView) {
            super(itemView);
            songdetail_item_layout = (RelativeLayout) itemView.findViewById(R.id.songdetail_item_layout);
            songdetail_item_pic = (ImageView) itemView.findViewById(R.id.songdetail_item_pic);
            songdetail_item_title = (TextView) itemView.findViewById(R.id.songdetail_item_title);
            songdetail_item_subTitle = (TextView) itemView.findViewById(R.id.songdetail_item_subTitle);
            songdetail_item_playerCounts = (TextView) itemView.findViewById(R.id.songdetail_item_playerCounts);
            songdetail_item_duration = (TextView) itemView.findViewById(R.id.songdetail_item_duration);
            songdetail_item_like = (TextView) itemView.findViewById(R.id.songdetail_item_like);
            songdetail_item_comment = (TextView) itemView.findViewById(R.id.songdetail_item_comment);

        }
    }
}
